# 斐波那契数列
def fib(n):
    if n < 2:
        return n
    else:
        return fib(n - 2) + fib(n - 1)


# print(fib(8))

# 快排
def quick(qlist):
    if len(qlist) == 0:
        return qlist
    else:
        qb = qlist[0]
        qa = quick([l for l in qlist[1:] if l <= qb])
        qc = quick([m for m in qlist[1:] if m > qb])
    return qa + [qb] + qc

# print(quick([32, 72, 34, 253, 25, 2]))


# 随机插入排序
def insertQuick(qlist):
    for i in range(1, len(qlist)):
        key = qlist[i]
        j = i -1
        while j >= 0 and key < qlist[j]:
            qlist[j + 1] = qlist[j]
            j -= 1
            qlist[j + 1] = key
    return qlist


# print(insertQuick([23, 1, 54, 2, 32,6]))

# 99乘法表
def n_n():
    for i in range(1, 10):
        for j in range(1, i + 1):
            print(f'{j}*{i}={j*i}', end=" ")
        print('\t')

# print(n_n())

# 统计字符串中出现次数最多的字符和次数
def countStr(str):
    d1 = {}
    for i in str:
        if i not in d1:
            d1[i] = 1
        else:
            d1[i] += 1
    for k, v in d1.items():
        if v == max(d1.values()):
            print(k, v)

# countStr("HJHjhjhnJAjhuihiuHI")

# Switch语句
def sc():
    dict = {
        "1": 111,
        "2": 222,
        "3": 333,
        "4": 444,
        "5": 555
    }
    userInput = input("请输入要选择的数字：\n")
    print(dict[userInput])


# sc()


# filter()筛选功能
def names(n):
    if n.startswith('刘'):
        return True

# print(list(filter(names, ["张什么", "赵什么", "刘什么", "王什么"])))
# map()映射方法
def add(i):
    return i +9


# print(list(map(add, [21, 31, 41, 51])))

# 获取list索引位置
def list1():
    list = [21, 31, 41, 51]
    print(list.index(31))

# print(list1())

# 判断字符串中大写的几个，小写的几个
def strBs(s):
    a = 0
    b = 0
    for i in s:
        if i.islower():
            a += 1
        else:
            b += 1
    return a, b

# print(strBs("SJDAdjkasd"))


# 修改元组内部元素不可修改
def tup():
    t1 = (1, 2, 3, [3, 4, 5, 67])
    t1[3].extend([123,123,412])
    print(t1)

# tup()
# 列表推导式
l1 = [i for i in range(15)]
print(l1)
# 集合推导式
s1 = {i for i in range(12)}
print(s1)
# 字典推导式
l2 = ["张", "刘", "李"]
dc = {i: l2[i] for i in range(len(l2))}
print(dc)
# 函数嵌套


# 迭代器
def iterb():
    s1 = "asdfghjkl"
    s2 = {1, 2, 3, 4}
    l1 = [1, 2, 3, 4, 5]
    ss1 = s1.__iter__()
    print(ss1.__next__())
    print(ss1.__next__())
    print(ss1.__next__())
    ss2 = s2.__iter__()
    print(ss2.__next__())
    print(ss2.__next__())
    print(ss2.__next__())
# iterb()
# 生成器
def func():
    a = 11
    b = 12
    c = yield a + b
    d = 23
    yield c + d



f = func()

print(f.__next__())
print(f.send(3))


# 装饰器
def wapper(name):
    i = 12
    def inner(*args, **kwargs):
        nonlocal i
        i = 12 + i
        rt = name(*args, **kwargs)
        print(args, kwargs)
        print(i)
        return rt
    return inner

@wapper  # game = wapper(game)
def game(user, password):
    print()
    print(f'开始执行目标函数')
    print(f"用户名是:{user}")
    print(f'密码是：{password}')
    print(f'目标函数执行完成')
    return "开始玩游戏了！"

# print(game(user='zkk', password="123"))